﻿// See https://aka.ms/new-console-template for more information
using System;
public abstract class Shape
{
    private string myId;
    public Shape(string s)
    {
        Id = s;
    }
    public string Id
    {
        get
        {
            return myId;
        }
        private set
        {
            myId = value;
        }
    }
    public abstract double Area
    {
        get;
    }
    public abstract bool IsShape
    {
        get;
    }
    public override string ToString()
    {
        return Id + "Area = " + String.Format("{0:F2}", Area);
    }
}
public class Square : Shape
{
    private int myside;
    public Square(int side, string id) : base(id)
    {
        myside = side;
    }
    public override double Area
    {
        get
        {
            return myside * myside;
        }
    }
    public override bool IsShape
    {
        get
        { return true; }
    }
}
public class Rectangle : Shape
{
    private int myWidth;
    private int myHeight;
    public Rectangle(int width, int heigth, string id) : base(id)
    {
        myWidth = width;
        myHeight = heigth;
    }
    public override double Area
    {
        get { return myWidth * myHeight; }
    }
    public override bool IsShape
    {
        get
        {
            return !(myWidth == myHeight);
        }
    }
}
public class Triangle : Shape
{
    private int x, y, z;
    private double p;
    public Triangle(int x, int y, int z, string id) : base(id)
    {
        this.x = x;
        this.y = y;
        this.z = z;
        p = 0.5 * (x + y + z);
    }
    public override double Area
    {
        get { return Math.Sqrt(p * (p - x) * (p - y) * (p - z)); }
    }

    public override bool IsShape
    {
        get
        {
            return (x + y > z) && (x + z > y) && (z + y > x);
        }
    }
}

public class Program
{
    public static Shape getShape(int id)
    {

        Shape ret = null;
        if (id == 1)
        {
            Random rand = new Random();
            int a = rand.Next(1, 20);
            ret = new Square(a, "正方形");
            Console.Write("创建了一个正方形！它的边长是：" + a + " 它的面积是：" + ret.Area + "\n");
        }
        else if (id == 2)
        {
            Random rand = new Random();
            int a = rand.Next(1, 20);
            int b = rand.Next(1, 20);
            int length, width = 0;
            if (a >= b)
            {
                length = a;
                width = b;
            }
            else
            {
                length = b;
                width = a;
            }

            ret = new Rectangle(a, b, "长方形");
            Console.Write("创建了一个长方形！它的长为 " + length + " 它的宽为 " + width + " 它的面积为 " + ret.Area + "\n");

        }
        else if (id == 3)
        {
            Random rand = new Random();
            int a = rand.Next(1, 20);
            int b = rand.Next(1, 20);
            int c = rand.Next(1, 20);
            ret = new Triangle(a, b, c, "三角形");
            if (ret.IsShape)
            {
                Console.Write("创建了一个符合规定的三角形！它的边长为" + a + " " + b + " " + c + " 它的面积是" + ret.Area + "\n");
            }
            else
            {
                Console.Write("由于三边为" + " " + a + " " + b + " " + c + " 创建三角形失败！\n");
            }
        }
        return ret;
    }
    public static void Main(string[] args)
    {
        for (int i = 0; i < 10; i++)
        {
            Random rand = new Random();
            int a = rand.Next(1, 4);
            Shape shape = getShape(a);
        }
    }

}


